package service

import (
	"context"
	"errors"
	"github.com/exam/model"
	"github.com/exam/model/dao"
	"github.com/go-redis/redis/v8"
)

type IOrderService interface {
	Find(ctx context.Context) ([]model.Order, error)
}

type OrderService struct {
	dao    *dao.OrderDao
	client *dao.OrderRedis
}

/*
响应更快
redis防击穿
*/
func (o *OrderService) Find(ctx context.Context) ([]model.Order, error) {
	key := "today:" + "orderCount"
	orderKey := "order_data"
	data, count := o.dao.Find()
	_, err := o.client.GetCount(ctx, key)
	if err != nil {
		if errors.Is(err, redis.ErrClosed) {

			o.client.Set(ctx, data, count, key, orderKey) //构建缓存
			return nil, err
		}
	}
	return data, err

}
func NewOrderService(dao *dao.OrderDao, client *dao.OrderRedis) IOrderService {
	return &OrderService{
		dao:    dao,
		client: client,
	}
}
